package pl.plesken.tools;

import java.io.File;
import java.util.LinkedList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import pl.plesken.xml.ConfigParser;


public class RunShowLogs {
        
        private static Pattern size = Pattern.compile("^Found (\\d*) movies in your media library$");
        private static Pattern movie = Pattern.compile("^Updating data for: (.*)$");
        private LinkedList<String> row = new LinkedList<String>();
        private LinkedList<String> collect = new LinkedList<String>();
        StringBuffer run = new StringBuffer();
        StringBuffer length = new StringBuffer();
        StringBuffer list = new StringBuffer();
        String dst;
        String execute;

        public String getDst() {
                return dst;
        }

        public void setDst(String dst) {
                this.dst = dst;
        }

		public String runSystem() {
                ConfigParser cnf = new ConfigParser();
                RunShowLogs List = new RunShowLogs();
                
                String path = cnf.readConf("GUIpath");
                list.setLength(0);
                List.List(path + File.separator + "libs" + File.separator + "moviejukebox");
                
                if (System.getProperty("os.name").contains("Windows")) { 
                        //run.append("java -Xms256m -Xmx512m -classpath " + "\"" + path + "\"" + ";" + "\"" + path + "\\resources\";" + "\"" + path + "\\libs\\moviejukebox\\moviejukebox.jar\";" + "\"" + path + "\\libs\\moviejukebox\\commons-logging-1.1.1.jar\";" + "\"" + path + "\\libs\\moviejukebox\\commons-lang-2.4.jar\";" + "\"" + path + "\\lib\\commons-configuration-1.5.jar\";" + "\"" + path + "\\lib\\commons-collections-3.2.1.jar\";" + "\"" + path + "\\lib\\filters.jar\";" + "\"" + path + "\\lib\\mucommanderlight.jar\";" + "\"" + path + "\\lib\\thetvdbapi.jar\";" + "\"" + path + "\\lib\\saxon9.jar\" com.moviejukebox.MovieJukebox");
                		for (int i = 0; i < List.collect.size(); i++) {
                			if (i == 0) {
                				list.append(List.collect.get(i).toString());
                			} else {
                				list.append(";" + List.collect.get(i).toString());
                			}
                		}
                		//System.out.println(list);
                		run.append("java -Xms256m -Xmx512m -classpath " + path + ";" + path + File.separator + "resources" + File.separator + ";" + list.toString() + " com.moviejukebox.MovieJukebox");
                } else {
                        //run.append("java -Xms256m -Xmx512m -classpath " + "\"" + path + "\"" + ":" + "\"" + path + "/resources\":" + "\"" + path + "/lib/moviejukebox.jar\":" + "\"" + path + "/lib/commons-logging-1.1.1.jar\":" + "\"" + path + "/lib/commons-lang-2.4.jar\":" + "\"" + path + "/lib/commons-configuration-1.5.jar\":" + "\"" + path + "/lib/commons-collections-3.2.1.jar\":" + "\"" + path + "/lib/filters.jar\":" + "\"" + path + "/lib/mucommanderlight.jar\":" + "\"" + path + "/lib/thetvdbapi.jar\":" + "\"" + path + "/lib/saxon9.jar\" com.moviejukebox.MovieJukebox");
                        //run.append("java -Xms256m -Xmx512m -classpath " + path + ":" + path + "/resources:" + path + "/lib/moviejukebox.jar:" + path + "/lib/commons-logging-1.1.1.jar:" + path + "/lib/commons-lang-2.4.jar:" + path + "/lib/commons-configuration-1.5.jar:" + path + "/lib/commons-collections-3.2.1.jar:" + path + "/lib/filters.jar:" + path + "/lib/mucommanderlight.jar:" + path + "/lib/thetvdbapi.jar:" + path + "/lib/saxon9.jar com.moviejukebox.MovieJukebox");
                		//run.append("java -Xms256m -Xmx512m -classpath .:./resources:./lib/* com.moviejukebox.MovieJukebox \"$@\"");
            			for (int i = 0; i < List.collect.size(); i++) {
            				if (i == 0) {
            					list.append(List.collect.get(i).toString());
            				} else {
            					list.append(":" + List.collect.get(i).toString());
            				}
            			}
            			run.append("java -Xms256m -Xmx512m -classpath " + path + ":" + path + "/resources:" + list.toString() + " com.moviejukebox.MovieJukebox");
                }

                System.out.println("System: " + System.getProperty("os.name")); //$NON-NLS-1$ //$NON-NLS-2$
                System.out.println(run + " " + path + File.separator + "libraries.xml -o \"" + dst + "\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
                execute = run + " " + path + File.separator + "libraries.xml -o " + dst;
                return execute;
        }
        
        public String findLength(String line) {
                length.setLength(0);
                row.add(line);
                Matcher s = size.matcher(row.getFirst());
                if (s.find()) {
                        length.append(s.group(1));
                }
                row.clear();
                return length.toString();
        }
        
        public Boolean findMovie(String line) {
                row.add(line);
                Matcher m = movie.matcher(row.getFirst());
                if (m.find()) {
                        row.clear();
                        return true;
                } else {
                        row.clear();
                        return false;
                }
        }
        
        private void List(String path) {
            File f = new File(path + File.separator);

            collect.clear();
            
            if (f.exists() && f.isDirectory()) {
                    String[] files = f.list();
                    for (int i = 0; i < files.length; i++) {                
                            File test = new File(path + File.separator + files[i]);
                            if (test.isFile()) {
                                    if (files[i].toLowerCase().endsWith(".jar")) {
                                    	collect.add(path + File.separator + files[i].toString());
                                    } 
                            }
                    } 
            }
        }
}

